package com.xxl.shoppingapp.ui.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xxl.baselib.util.ToastUtils;
import com.xxl.shoppingapp.R;
import com.xxl.shoppingapp.base.BaseFragment;
import com.xxl.shoppingapp.base.BaseRecyclerViewAdapter;
import com.xxl.shoppingapp.base.CommonActivity;
import com.xxl.shoppingapp.entity.CommonProduct;
import com.xxl.shoppingapp.ui.adapter.HomeListAdapter;
import com.xxl.shoppingapp.ui.adapter.LocalImageHolderView;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author xuelian.xiong 2018/4/10
 * @Package com.xxl.shoppingapp.ui.main
 * @Title: GoodsFragment
 * @Description: (产品列表页)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2018/4/10
 */
public class GoodsFragment extends BaseFragment {

    @Bind(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    @Bind(R.id.hotSearch)
    ImageButton hotSearch;
    @Bind(R.id.SearchBtn)
    ImageButton SearchBtn;
    @Bind(R.id.searchlayout)
    LinearLayout searchlayout;
    @Bind(R.id.iv_leaderboard)
    ImageView ivLeaderboard;
    @Bind(R.id.rl_rm)
    RelativeLayout rlRm;
    @Bind(R.id.iv_installed)
    ImageView ivInstalled;
    @Bind(R.id.installe)
    RelativeLayout installed;
    @Bind(R.id.new_product_count)
    TextView newProductCount;
    @Bind(R.id.iv_new_update)
    ImageView ivNewUpdate;
    @Bind(R.id.rl_qd)
    RelativeLayout rlQd;
    @Bind(R.id.iv_update)
    ImageView ivUpdate;
    @Bind(R.id.tv_update)
    TextView tvUpdate;
    @Bind(R.id.home_upgrade_count)
    TextView homeUpgradeCount;
    @Bind(R.id.rl_beautiful)
    RelativeLayout rlBeautiful;
    @Bind(R.id.my_traces_count)
    TextView myTracesCount;
    @Bind(R.id.iv_aftersingle)
    ImageView ivAftersingle;
    @Bind(R.id.rl_ms)
    RelativeLayout rlMs;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    com.scwang.smartrefresh.layout.SmartRefreshLayout refreshLayout;
    private int mType;
    private static String TYPE = "GoodsFragment.class";

    private ArrayList<Integer> localImages = new ArrayList<Integer>();
    ArrayList<CommonProduct> list = new ArrayList<>();
    HomeListAdapter adapter;
    static GoodsFragment instance;

    public synchronized static GoodsFragment getInstance() {
//        if (instance == null) {
//            instance = new GoodsFragment();
//        }
//        Bundle args = new Bundle();
//        args.putSerializable(TYPE, type);
//        instance.setArguments(args);
        return new GoodsFragment();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_goods;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        Bundle args = this.getArguments();
        if (args != null)
            mType = (int) args.getSerializable(TYPE);


    }

    private void setListView() {
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new HomeListAdapter();
        adapter.setItemClickListener(new BaseRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastUtils.show("position:" + position);
            }
        });
        recyclerView.setAdapter(adapter);
        adapter.addData(list);
        adapter.notifyDataSetChanged();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                addData();
                adapter.setData(list);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                addData();
                adapter.addData(list);
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        addData();
        setBanner();
        setListView();
    }

    void setBanner() {
        if (localImages.size() < 3) {
            for (int position = 2; position < 5; position++) {
                localImages.add(getResId("banner_img" + position, R.mipmap.class));
            }
        }

        //开始自动翻页
        convenientBanner.setPages(new CBViewHolderCreator() {
            @Override
            public Object createHolder() {
                return new LocalImageHolderView();
            }
        }, localImages)
                //设置指示器是否可见
                .setPointViewVisible(true)
                //设置自动切换（同时设置了切换时间间隔）
                .startTurning(2000)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.mipmap.banner_dian_focus, R.mipmap.banner_dian_blur})
                //设置指示器的方向（左、中、右）
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                //设置点击监听事件
//                .setOnItemClickListener(this)
                //设置手动影响（设置了该项无法手动切换）
                .setManualPageable(true);

    }

    public static int getResId(String variableName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }


    @OnClick({R.id.searchlayout, R.id.rl_rm, R.id.installe, R.id.rl_qd, R.id.rl_beautiful, R.id.rl_ms})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.searchlayout:
                break;
            case R.id.rl_rm:
                CommonActivity.showFromFragment(this, ProductListFragment.getInstance(0));
                break;
            case R.id.installe:
                CommonActivity.showFromFragment(this, ProductListFragment.getInstance(4));
                break;
            case R.id.rl_qd:
                CommonActivity.showFromFragment(this, ProductListFragment.getInstance(1));
                break;
            case R.id.rl_beautiful:
                CommonActivity.showFromFragment(this, ProductListFragment.getInstance(2));
                break;
            case R.id.rl_ms:
                CommonActivity.showFromFragment(this, ProductListFragment.getInstance(3));
                break;
        }
    }

    void addData() {
        for (int i = 0; i < 6; i++) {
            CommonProduct produc = new CommonProduct();
            produc.setProductName("日式便当盒" + i);
            list.add(produc);
        }
    }

}
